<template>
	<div class="dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" :close-on-click-modal="false" width="1000px">
			<el-form ref="formRef" :model="state.ruleForm" :rules="state.rules" :disabled="state.dialog.type === 'view' ? true : false">
				<el-row class="mb20">
					<el-col :span="24">
						<el-form-item label="轮播图标题" prop="title">
							<el-input v-model="state.ruleForm.title" placeholder="请输入轮播图标题" clearable />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row class="mb20">
					<el-col :span="12">
						<el-form-item label="轮播图(默认图)" prop="thumb">
							<ImageUploadCOS v-model="state.ruleForm.thumb" image-size="710*290" path="banner" :is-dialog-upload="state.dialog.isShowDialog" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="跳转类型" prop="link_type">
							<el-radio-group v-model="state.ruleForm.link_type">
								<el-radio :value="0">不跳转</el-radio>
								<el-radio :value="1">跳转至商品</el-radio>
								<el-radio :value="2">跳转至商品标签</el-radio>
							</el-radio-group>
						</el-form-item>
						<el-form-item label="排序" prop="display_order">
							<el-input v-model="state.ruleForm.display_order" placeholder="请输入排序" clearable @input="onVerifiyNumberIntegerDisplayOrder" />
						</el-form-item>
						<el-form-item v-if="state.ruleForm.link_type === 1" label="关联商品" prop="goods_id">
							<el-select
								v-model="state.ruleForm.goods_id"
								clearable
								placeholder="请输入关键字/选择商品名称"
								filterable
								remote
								:remote-method="goodsIdRemoteMethod"
								remote-show-suffix
								:loading="state.goodsIdSelect.loading"
								@focus="onGoodsIdFocus"
							>
								<el-option v-for="item in state.goodsIdSelect.options" :key="item.id" :label="item.title" :value="item.id" />
							</el-select>
						</el-form-item>
						<el-form-item v-else-if="state.ruleForm.link_type === 2" label="关联商品标签" prop="goods_tags_id">
							<el-select
								v-model="state.ruleForm.goods_tags_id"
								clearable
								placeholder="请输入关键字/选择商品名称"
								filterable
								remote
								:remote-method="goodsTagsIdRemoteMethod"
								remote-show-suffix
								:loading="state.goodsTagsIdSelect.loading"
								@focus="onGoodsTagsIdFocus"
							>
								<el-option v-for="item in state.goodsTagsIdSelect.options" :key="item.id" :label="item.name" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row class="mb20">
					<el-col :span="12">
						<el-form-item label="轮播图(英文图)" prop="thumb_en">
							<ImageUploadCOS v-model="state.ruleForm.thumb_en" image-size="710*290" path="banner" :is-dialog-upload="state.dialog.isShowDialog" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="轮播图(老挝图)" prop="thumb_lo">
							<ImageUploadCOS v-model="state.ruleForm.thumb_lo" image-size="710*290" path="banner" :is-dialog-upload="state.dialog.isShowDialog" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<el-button @click="onCancel" size="default">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :loading="state.dialog.submitLoading" size="default">提 交</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus';
import { nextTick, reactive, ref } from 'vue';
import { bannerApi, goodsApi, goodsTagsApi } from '/@/api';
import { verifiyNumberInteger } from '/@/utils/toolsValidate';

let callback: Function = () => {};
const BannerApi = bannerApi();
const GoodsApi = goodsApi();
const GoodsTagsApi = goodsTagsApi();
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	},
	ruleForm: {
		title: '',
		thumb: '',
		thumb_en: '',
		thumb_lo: '',
		link_type: 0,
		link_content: '',
		goods_id: '',
		goods_tags_id: '',
		display_order: '',
	},
	rules: {
		title: [{ required: true, message: '轮播图标题不能为空', trigger: ['blur'] }],
		thumb: [{ required: true, message: '轮播图不能为空', trigger: ['blur', 'change'] }],
		goods_id: [{ required: true, message: '关联商品不能为空', trigger: ['blur', 'change'] }],
		goods_tags_id: [{ required: true, message: '关联商品标签不能为空', trigger: ['blur', 'change'] }],
	},
	goodsIdSelect: {
		loading: false,
		options: [] as any,
	},
	goodsTagsIdSelect: {
		loading: false,
		options: [] as any,
	},
});
const formRef = ref();

/********** 触发事件 **********/
// 关联商品获取焦点
const onGoodsIdFocus = () => {
	if (state.ruleForm.goods_id) return;
	_goodsList({ pageNum: 1, pageSize: 10 });
};
// 关联商品标签获取焦点
const onGoodsTagsIdFocus = () => {
	if (state.ruleForm.goods_tags_id) return;
	_goodsTagsList({ pageNum: 1, pageSize: 10 });
};
// 排序过滤
const onVerifiyNumberIntegerDisplayOrder = (val: string) => {
	state.ruleForm.display_order = verifiyNumberInteger(val);
};
// 取消
const onCancel = () => {
	state.dialog = {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	};
};
// 提交
const onSubmit = () => {
	formRef.value.validate((valid: boolean) => {
		if (!valid) return;
		let link_content = {};
		if (state.ruleForm.link_type === 1) {
			const goodsInfo = state.goodsIdSelect.options.find((item: any) => item.id === state.ruleForm.goods_id);
			link_content = { id: goodsInfo.id, title: goodsInfo.title };
		} else if (state.ruleForm.link_type === 2) {
			const goodsTagsInfo = state.goodsTagsIdSelect.options.find((item: any) => item.id === state.ruleForm.goods_tags_id);
			link_content = { id: goodsTagsInfo.id, name: goodsTagsInfo.name };
		}
		state.ruleForm.link_content = JSON.stringify(link_content);
		state.ruleForm.thumb_en = state.ruleForm.thumb_en || state.ruleForm.thumb;
		state.ruleForm.thumb_lo = state.ruleForm.thumb_lo || state.ruleForm.thumb;
		state.dialog.submitLoading = true;
		if (state.dialog.type === 'add') {
			_bannerAdd(state.ruleForm);
		} else {
			_bannerEdit(state.ruleForm);
		}
	});
};

/********** 方法事件 **********/
// 打开
const openDialog = (data: any, row: any, fn: Function) => {
	state.dialog = {
		isShowDialog: true,
		type: data.type,
		title: data.title,
		submitLoading: false,
	};
	callback = fn;
	nextTick(() => {
		formRef.value.resetFields();
		if (state.dialog.type !== 'add') {
			_bannerInfo({ banner_id: row.id });
		}
	});
};
// 关联商品搜索选择
const goodsIdRemoteMethod = (query: string) => {
	if (!query) return;
	_goodsList({ pageNum: 1, pageSize: 10, title: query });
};
// 关联商品标签搜索选择
const goodsTagsIdRemoteMethod = (query: string) => {
	if (!query) return;
	_goodsTagsList({ pageNum: 1, pageSize: 10, name: query });
};

/********** 网络请求 **********/
// 添加轮播图
const _bannerAdd = (params: Object) => {
	BannerApi.bannerAdd(params)
		.then(() => {
			ElMessage.success('操作成功！');
			onCancel();
			callback();
		})
		.catch(() => (state.dialog.submitLoading = false));
};
// 编辑轮播图
const _bannerEdit = (params: Object) => {
	BannerApi.bannerEdit(params)
		.then(() => {
			ElMessage.success('操作成功！');
			onCancel();
			callback();
		})
		.catch(() => (state.dialog.submitLoading = false));
};
// 获取轮播图信息
const _bannerInfo = (params: Object) => {
	BannerApi.bannerInfo(params).then((res) => {
		state.ruleForm = {
			...res.data.info.item,
			banner_id: res.data.info.item.id,
		};
		let link_content = JSON.parse(state.ruleForm.link_content);
		if (state.ruleForm.link_type === 1) {
			state.ruleForm.goods_id = link_content.id;
			state.goodsIdSelect.options = [link_content];
		} else if (state.ruleForm.link_type === 2) {
			state.ruleForm.goods_tags_id = link_content.id;
			state.goodsTagsIdSelect.options = [link_content];
		}
	});
};
// 商品列表
const _goodsList = (params: Object) => {
	state.goodsIdSelect.loading = true;
	GoodsApi.goodsList(params)
		.then((res) => {
			state.goodsIdSelect.options = res.data.list;
			state.goodsIdSelect.loading = false;
		})
		.catch(() => (state.goodsIdSelect.loading = false));
};
// 商品标签列表
const _goodsTagsList = (params: Object) => {
	state.goodsTagsIdSelect.loading = true;
	GoodsTagsApi.goodsTagsList(params)
		.then((res) => {
			state.goodsTagsIdSelect.options = res.data.list;
			state.goodsTagsIdSelect.loading = false;
		})
		.catch(() => (state.goodsTagsIdSelect.loading = false));
};

/********** 方法向父组件暴露 **********/
defineExpose({ openDialog });
</script>
